var isWorker;
try {
    isWorker = !(self instanceof SharedWorkerGlobalScope);

} catch (error) {
    // isWorker = !(self instanceof SharedWorkerGlobalScope);
    isWorker = true;
}
if (isWorker) {
    onmessage = function (e) {
        var data = e.data;
        postMessage(data);
    }

} else {
    var ports = [];
    var persistCacheMap = {};
    addEventListener('connect', function (event) {
        var port = event.ports[0];
        ports.push(port);
        port.start();
        port.addEventListener("message", function (event) {
            var context = event.data;
            if (context.persistMsg) {
                //persist context
                persistCacheMap[context.contextClass] = context;
            }
            if (context.contextClass == "PersitContext") {
                context.persistContext = persistCacheMap[context.persitContextClass];
                port.postMessage(context);
            } else {
                ports.forEach(function (port) {
                    port.postMessage(context);
                });
            }
        });
    });

}